using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SpeechLib;
using Microsoft.Win32;

namespace TelliTwitter
{
    public partial class OptionsForm : Form
    {
        public OptionsForm()
        {
            InitializeComponent();
            this.Load += new EventHandler(OptionsForm_Load);
        }

        void OptionsForm_Load(object sender, EventArgs e)
        {
            SetupVoiceDDL();
            LoadOptions();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text.Trim() == string.Empty || txtPassword.Text.Trim() == string.Empty)
            {
                MessageBox.Show("User name and password are required.", "Account Details required", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            int delay = 0;
            if (txtRefreshDelay.Text.Trim() == string.Empty || !int.TryParse(txtRefreshDelay.Text.Trim(), out delay))
            {
                MessageBox.Show("Refresh Delay is required and must be a valid number.", "Refresh Delay required", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                return;
            }
            SaveOptions();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            SaveOptions();
        }

        private void SetupVoiceDDL()
        {
            ddlVoice.Items.Insert(0, "Default");
            SpVoice Voice = new SpVoice();
            foreach (ISpeechObjectToken Token in Voice.GetVoices(string.Empty, string.Empty))
            {
                ddlVoice.Items.Add(Token.GetDescription(49));
            }
        }

        private void LoadOptions()
        {
            txtUserName.Text = Properties.Settings.Default.UserName;
            txtPassword.Text = Properties.Settings.Default.Password;
            txtRefreshDelay.Text = Properties.Settings.Default.RefreshDelay.ToString();
            chkVoice.Checked = Properties.Settings.Default.VoiceEnabled;
            ddlVoice.SelectedItem = Properties.Settings.Default.SelectedVoice;
            chkMinimizeToTray.Checked = Properties.Settings.Default.MinimizeToTray;
            chkPlaySound.Checked = Properties.Settings.Default.PlaySound;
            trackBarVoiceVolume.Value = Properties.Settings.Default.VoiceVolume;
            trackBarVoiceRate.Value = Properties.Settings.Default.VoiceRate;
            chkStartAtLogin.Checked = (((string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", "TeleTwitter", "")).Length > 0);
            radJsonButton.Checked = (Properties.Settings.Default.ApiType == (int)TelliTwitter.Lib.ApiType.JSON);
            radXmlButton.Checked = (Properties.Settings.Default.ApiType == (int)TelliTwitter.Lib.ApiType.XML);
        }

        private void SaveOptions()
        {
            Properties.Settings.Default.UserName = txtUserName.Text;
            Properties.Settings.Default.Password = txtPassword.Text;
            Properties.Settings.Default.RefreshDelay = int.Parse(txtRefreshDelay.Text.Trim());
            Properties.Settings.Default.VoiceEnabled = chkVoice.Checked;
            Properties.Settings.Default.SelectedVoice = ddlVoice.SelectedItem.ToString();
            Properties.Settings.Default.MinimizeToTray = chkMinimizeToTray.Checked;
            Properties.Settings.Default.PlaySound = chkPlaySound.Checked;
            Properties.Settings.Default.VoiceVolume = trackBarVoiceVolume.Value;
            Properties.Settings.Default.VoiceRate = trackBarVoiceRate.Value;

            if (chkStartAtLogin.Checked != (((string)Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", "TeleTwitter", "")).Length > 0))
            {
                if (chkStartAtLogin.Checked)
                {
                    Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", "TeleTwitter", System.Reflection.Assembly.GetEntryAssembly().Location);
                }
                else
                {
                    Microsoft.Win32.Registry.SetValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run", "TeleTwitter", "");
                }
            }

            Properties.Settings.Default.ApiType = radJsonButton.Checked ? (int)TelliTwitter.Lib.ApiType.JSON : (int)TelliTwitter.Lib.ApiType.XML;
            TelliTwitter.Lib.TwitterManager.Instance().TwitterApi = (TelliTwitter.Lib.ApiType)Properties.Settings.Default.ApiType;

            Properties.Settings.Default.Save();
        }

    }
}